<?php

class PromotionController extends BaseController
{
	
	/**
	 * 满就送管理
	 */
	public function actionAdmin()
	{
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',Company::model()->getCmpUserId());//当前会员
		$criteria->compare('zcr_end_time','>'.time());//没结束的活动
		$criteria->compare('zcr_status',array(1,2));//状态不为关闭
		$criteria->compare('zcr_type',array(1,2,3,4));
		$model = CmpPromotion::model()->findAll($criteria);
		$this->render('meetgive',array(
				'model'=>$model,
				));
	
	}
	
	/**
	 * 创建满就送
	 */
	public function actionCreateMeetGive(){
		$model = new CmpPromotion();
		
		if(isset($_POST['CmpPromotion'])){
			$model->attributes = $_POST['CmpPromotion'];
			$model->zcr_start_time = strtotime($model->zcr_start_time);
			$model->zcr_end_time = strtotime($model->zcr_end_time);
			if($model->save()){
				//成功跳转
				$this->redirect(array($this->pre_module_url.'/promotion/admin'));
			}
			print_r($model->zm_id);exit;
		}
		
		$this->render('meetgive_create',array(
				'model'=>$model,
		));
	}
	
	/**
	 * 修改满就送
	 */
	public function actionUpdateMeetGive($id){
		$model = CmpPromotion::model()->findByPk($id);
		$model->zcr_start_time = date('Y-m-d',$model->zcr_start_time);
		$model->zcr_end_time = date('Y-m-d',$model->zcr_end_time);
		
		if(isset($_POST['CmpPromotion'])){
			$model->attributes = $_POST['CmpPromotion'];
			$model->zcr_start_time = strtotime($model->zcr_start_time);
			$model->zcr_end_time = strtotime($model->zcr_end_time);
			if($model->save()){
				//成功跳转
				$this->redirect(array($this->pre_module_url.'/promotion/admin'));
			}
		}
		
		$this->render('meetgive_update',array(
				'model'=>$model,
		));
	}
	
	/**
	 * 启动停止满就送，
	 */
	public function actionChangeMeetGive($id){
		$model = CmpPromotion::model()->findByPk($id);
		if($model->zcr_status==1){
			$model->zcr_status = 2;//状态  是否关闭 1否，2是
		}elseif($model->zcr_status==2){
			$model->zcr_status = 1;//状态  是否关闭 1否，2是
		}else{
			throw new CHttpException(404,'The requested page does not exist.');
		}
	
		if($model->save()){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}

	/**
	 * 取消满就送，取消优惠
	 */
	public function actionDeleteMeetGive($id){
		$model = CmpPromotion::model()->findByPk($id);
		$model->zcr_status = 3;//状态  是否关闭 1否，2是，3删除
	
		if($model->save()){
			//成功跳转
			$this->redirect(array($this->pre_module_url.'/promotion/admin'));
		}
	}
	
	/**
	 * 套餐搭配管理
	 */
	public function actionPackage(){
		$model = new ProPackage('search');
		$model->unsetAttributes();
		$model->zm_id = Company::model()->getCmpUserId();//当前会员
		$model->zpp_status = array(1,2);//状态不为删除
		
		if(isset($_GET['ProPackage']))
			$model->attributes = $_GET['ProPackage'];
		
		$this->render('package',array(
				'model'=>$model,
		));
	}
	
	/**
	 * 新建套餐搭配
	 */
	public function actionCreatePackage(){
		$model = new ProPackage();
		
		$product = New Product();
		$product->unsetAttributes();//清空属性
		$product->zm_id = Company::model()->getCmpUserId();
		
		if(isset($_POST['ProPackage'])){
			$model->attributes = $_POST['ProPackage'];
			if($model->save()){
				$this->render('../index/success',array(
						'message'=>'恭喜，套餐创建成功',
						'links'=>array(
								array('套餐管理页面',$this->pre_module_url.'/promotion/package'),
								array('查看创建的套餐',$this->pre_module_url.'/promotion/viewPackage/id/'.$model->zpp_id),
						),
				));
				exit;
			}
		}
		
		if(isset($_GET['Product']))
			$product->attributes = $_GET['Product'];
		
		$this->render('package_create',array(
				'model'=>$model,
				'product'=>$product,
		));
	}
	
	/**
	 * 修改套餐搭配
	 */
	public function actionUpdatePackage($id){
		$model = ProPackage::model()->findByPk($id);
		
		$product = New Product();
		$product->unsetAttributes();//清空属性
		$product->zm_id = Company::model()->getCmpUserId();
		
		if(isset($_POST['ProPackage'])){
			$model->attributes = $_POST['ProPackage'];
			if($model->save()){
				$this->render('../index/success',array(
						'message'=>'恭喜，套餐创建成功',
						'links'=>array(
								array('套餐管理页面',$this->pre_module_url.'/promotion/package'),
								array('查看创建的套餐',$this->pre_module_url.'/promotion/viewPackage/id/'.$model->zpp_id),
						),
				));
				exit;
			}
		}
		
		$this->render('package_update',array(
				'model'=>$model,
				'product'=>$product,
		));
	}
	
	/**
	 * 激活套餐搭配
	 */
	public function actionActivePackage($id){
		$model = ProPackage::model()->findByPk($id);
		foreach(explode(',',$model->zp_ids) as $val){
			$product = Product::model()->findByPk($val);
			if($product->zp_inventory<=0){
				$this->render('../index/error',array('message'=>'恭喜，套餐激活失败，“'.$product->zp_name.'”产品库存为零，请补允库存'));
			}
		}
		if($model->zpp_status==2){
			$model->zpp_status = 1;
			if($model->save()){
				//成功跳转
				$this->redirect(Yii::app()->request->urlReferrer);
			}
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
	}
	
	/**
	 * 查看套餐搭配
	 */
	public function actionViewPackage(){
	
	}
	
	/**
	 * 删除套餐搭配
	 */
	public function actionDeletePackage($id){
		$model = ProPackage::model()->findByPk($id);
		$model->zpp_status = 3;
		if($model->save()){
			//成功跳转
			$this->redirect(array($this->pre_module_url.'/promotion/package'));
		}
	}
	
	/**
	 * 限时折扣管理
	 */
	public function actionDiscount(){
		$model = new CmpPromotion('search');
		$model->unsetAttributes();//清除属性
		$model->zm_id = Company::model()->getCmpUserId();//当前会员
		$model->zcr_status = array(1,2);//状态不为删除
		$model->zcr_type = 5;//类型：1.限时折扣 2抢购秒杀',
		
		if(isset($_GET['ProDiscount']))
			$model->attributes = $_GET['ProDiscount'];
		
		$this->render('discount',array(
					'model'=>$model,
		));
	}
	
	/**
	 * 创建限时折扣
	 */
	public function actionCreateDiscount(){
		$model = new CmpPromotion();
		$model->zcr_type=5;//类型：1.限时折扣活动',
		
		$product = New Product();
		$product->unsetAttributes();//清空属性
		$product->zm_id = Company::model()->getCmpUserId();
		$product->butPromotion = true;
		if(isset($_GET['Product']))
			$product->attributes = $_GET['Product'];
		
		if(isset($_POST['CmpPromotion'])){
			$model->attributes = $_POST['CmpPromotion'];
			$model->zcr_start_time = strtotime($model->zcr_start_time.":00");
			$model->zcr_end_time = strtotime($model->zcr_end_time.":00");
			if(!CmpPromotion::model()->CheckPromotion($_POST['ProDiscount']['zp_id'])){
				$this->render('../index/error',array(
						'message'=>'对不起，所选产品已有其它促销活动，一种商品不能同时参与两种促销活动，请确认',
						'links'=>array(
								array('套餐管理页面',$this->pre_module_url.'/promotion/discount'),
						),
				));
				exit;
			}
			if($model->save()){
				$discount = $_POST['ProDiscount'];
				//创建折扣产品
				foreach($discount['zp_id'] as $key=>$val){
					$proDiscount = new ProDiscount();
					$proDiscount->zp_id = $val;
					$proDiscount->zm_id = $model->zm_id;
					$proDiscount->zcr_id = $model->zcr_id;
					$proDiscount->zpd_start_time = $model->zcr_start_time;
					$proDiscount->zpd_end_time = $model->zcr_end_time;
					$proDiscount->zpd_discount = $discount['zpd_discount'][$key];
					$proDiscount->zpd_status = 1;
					$proDiscount->inputtime = time();
					if(!$proDiscount->save()){
						$this->render('../index/error',array(
								'message'=>'对不起，限时折扣创建失败，请稍后再试',
								'links'=>array(
										array('套餐管理页面',$this->pre_module_url.'/promotion/discount'),
								),
						));
						exit;
					}
				}
				//成功跳转
				$this->redirect(array($this->pre_module_url.'/promotion/discount'));
			}
		}

		$this->render('discount_create',array(
				'model'=>$model,
				'product'=>$product,
		));
	}
	
	/**
	 * 修改限时折扣
	 */
	public function actionUpdateDiscount($id){
		$model = CmpPromotion::model()->findByPk($id);
		$model->zcr_start_time = date('Y-m-d H',$model->zcr_start_time);
		$model->zcr_end_time = date('Y-m-d H',$model->zcr_end_time);
		
		$product = New Product();
		$product->unsetAttributes();//清空属性
		$product->zm_id = Company::model()->getCmpUserId();
		if(isset($_GET['Product']))
			$product->attributes = $_GET['Product'];
		
		if(isset($_POST['CmpPromotion'])){
			$model->attributes = $_POST['CmpPromotion'];
			$model->zcr_start_time = strtotime($model->zcr_start_time.":00");
			$model->zcr_end_time = strtotime($model->zcr_end_time.":00");
			if($model->save()){
				$discount = $_POST['ProDiscount'];
				//删除旧的折扣产品
				ProDiscount::model()->deleteAllByAttributes(array('zcr_id'=>$model->zcr_id));
				//创建折扣产品
				foreach($discount['zp_id'] as $key=>$val){
					$proDiscount = new ProDiscount();
					$proDiscount->zp_id = $val;
					$proDiscount->zm_id = $model->zm_id;
					$proDiscount->zcr_id = $model->zcr_id;
					$proDiscount->zpd_start_time = $model->zcr_start_time;
					$proDiscount->zpd_end_time = $model->zcr_end_time;
					$proDiscount->zpd_discount = $discount['zpd_discount'][$key];
					$proDiscount->zpd_status = 1;
					$proDiscount->inputtime = time();
					if(!$proDiscount->save()){
						$this->render('../index/error',array(
								'message'=>'对不起，限时折扣创建失败，请稍后再试',
								'links'=>array(
										array('套餐管理页面',$this->pre_module_url.'/promotion/discount'),
								),
						));
						exit;
					}
				}
				//成功跳转
				$this->redirect(array($this->pre_module_url.'/promotion/discount'));
			}
		}
		
		$this->render('discount_create',array(
				'model'=>$model,
				'product'=>$product,
		));
	}
	
	/**
	 * 删除限时折扣
	 * MIAO
	 */
	public function actionDeleteDiscount($id){
		
		$model = CmpPromotion::model()->findByPk($id);
		
		if($model->zcr_status == 2||$model->zcr_end_time<time()){
			//删除旧的折扣产品
			ProDiscount::model()->deleteAllByAttributes(array('zcr_id'=>$id));
			$model->zcr_status = 3;//是否关闭1否2是3删除
		}else{
			throw new CHttpException(404,'该活动还未关闭，不可删除！');
		}
		if($model->save()){
			//成功跳转
			$this->redirect(array($this->pre_module_url.'/promotion/discount'));
		}
	}
	 
	/**
	 * 更改状态限时折扣，停止，启用
	 */
	public function actionChangeDiscount($id){
		$model = CmpPromotion::model()->findByPk($id);
		if($model->zcr_status==1){
			$model->zcr_status = 2;//状态  是否关闭 1否，2是
		}elseif($model->zcr_status==2){
			$model->zcr_status = 1;
		}
		
		
		if($model->save()){
			$criteria=new CDbCriteria;
			$criteria->compare('zcr_id',$model->zcr_id);
			ProDiscount::model()->updateAll(array('zpd_status'=>$model->zcr_status),$criteria);
			
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}
	
	/**
	 * 团购活动管理
	 */
	public function actionTuan(){
		$model = new ProTuan('search');
		$model->unsetAttributes();//清除属性
		$model->zm_id = Company::model()->getCmpUserId();//当前会员
		$model->zpt_type = 1;//类型:1:团购2:秒杀3:限量抢购
		$model->zpt_close = array(1,2);//状态不为删除
		
		if(isset($_GET['ProTuan']))
			$model->attributes = $_GET['ProTuan'];
		
		$this->render('tuan',array(
				'model'=>$model,
		));
	}
	
	/**
	 * 创建团购活动
	 */
	public function actionCreateTuan(){
		$model = new ProTuan();
		$model->zpt_type = 1;//类型:1:团购2:秒杀3:限量抢购
		
		$product = New Product();
		$product->unsetAttributes();//清空属性
		$product->butPromotion = true;
		if(isset($_GET['Product']))
			$product->attributes = $_GET['Product'];
		
		if(isset($_POST['ProTuan'])){
			$model->attributes = $_POST['ProTuan'];
			$model->zpt_start_time = strtotime($model->zpt_start_time);
			$model->zpt_end_time = strtotime($model->zpt_end_time);
			if(!CmpPromotion::model()->CheckPromotion($model->zp_id)){
				$this->render('../index/error',array(
						'message'=>'对不起，所选产品已有其它促销活动，一种商品不能同事参与两种促销活动，请确认',
						'links'=>array(
								array('套餐管理页面',$this->pre_module_url.'/promotion/tuan'),
						),
				));
				exit;
			}
			if($model->save()){
				//成功跳转
				$this->redirect(array($this->pre_module_url.'/promotion/tuan'));
			}else{
				print_r($model);
			}
		}
		
		$this->render('tuan_create',array(
				'model'=>$model,
				'product'=>$product,
		));
	}
	
	/**
	 * 修改团购活动
	 */
	public function actionUpdateTuan($id){
		$model = ProTuan::model()->findByPk($id);
		$model->zpt_start_time = date('Y-m-d',$model->zpt_start_time);
		$model->zpt_end_time = date('Y-m-d',$model->zpt_end_time);
		
		$product = New Product();
		$product->unsetAttributes();//清空属性
		$product->zm_id = Company::model()->getCmpUserId();
		if(isset($_GET['Product']))
			$product->attributes = $_GET['Product'];
		
		if(isset($_POST['ProTuan'])){
			$model->attributes = $_POST['ProTuan'];
			$model->zpt_start_time = strtotime($model->zpt_start_time);
			$model->zpt_end_time = strtotime($model->zpt_end_time);
			if($model->save()){
				//成功跳转
				$this->redirect(array($this->pre_module_url.'/promotion/tuan'));
			}
		}
		
		$this->render('tuan_create',array(
				'model'=>$model,
				'product'=>$product,
		));
	}
	
	/**
	 * 启动停止团购
	 */
	public function actionChangeTuan($id){
		$model = ProTuan::model()->findByPk($id);
	
		if($model->zpt_close == 1){
			$model->zpt_close = 2;
		}elseif($model->zpt_close == 2){
			$model->zpt_close = 1;
		}
		if($model->save()){
			//成功跳转
			 $this->redirect(Yii::app()->request->urlReferrer);
		}
	}
	
	/**
	 * 删除团购活动
	 */
	public function actionDeleteTuan($id){
		$model = ProTuan::model()->findByPk($id);
		
		if($model->zpt_close == 2||$model->zpt_end_time<time()){
			$model->zpt_close = 3;//是否关闭1否2是3删除
		}else{
			throw new CHttpException(404,'该活动还未关闭，不可删除！');
		}
		if($model->save()){
			//成功跳转
			$this->redirect(array($this->pre_module_url.'/promotion/tuan'));
		}
	}
	
	/**
	 * 抢购秒杀管理
	 */
	public function actionSnapup(){
		$model = new ProTuan('search');
		$model->unsetAttributes();//清除属性
		$model->zm_id = Company::model()->getCmpUserId();//当前会员
		$model->zpt_type = 2;//类型:1:团购2:秒杀3:限量抢购
		$model->zpt_close = array(1,2);//状态不为删除
		
		if(isset($_GET['ProTuan']))
			$model->attributes = $_GET['ProTuan'];
		
		$this->render('snapup',array(
				'model'=>$model,
		));
	}
	
	/**
	 * 创建抢购秒杀
	 */
	public function actionCreateSnapup(){
		$model = new ProTuan();
		$model->zpt_type = 2;//类型:1:团购2:秒杀3:限量抢购
		
		$product = New Product();
		$product->unsetAttributes();//清空属性
		$product->butPromotion = true;
		if(isset($_GET['Product']))
			$product->attributes = $_GET['Product'];
		
		if(isset($_POST['ProTuan'])){
			$model->attributes = $_POST['ProTuan'];
			$model->zpt_start_time = strtotime($model->zpt_start_time);
			$model->zpt_end_time = strtotime($model->zpt_end_time);
			if(!CmpPromotion::model()->CheckPromotion($model->zp_id)){
				$this->render('../index/error',array(
						'message'=>'对不起，所选产品已有其它促销活动，一种商品不能同事参与两种促销活动，请确认',
						'links'=>array(
								array('抢购秒杀管理页面',$this->pre_module_url.'/promotion/snapup'),
						),
				));
				exit;
			}
			if($model->save()){
				//成功跳转
				$this->redirect(array($this->pre_module_url.'/promotion/snapup'));
			}else{
				print_r($model);
			}
		}
		
		$this->render('snapup_create',array(
				'model'=>$model,
				'product'=>$product,
		));
	}
	
	/**
	 * 修改抢购秒杀
	 */
	public function actionUpdateSnapup($id){
		$model = ProTuan::model()->findByPk($id);
		$model->zpt_start_time = date('Y-m-d',$model->zpt_start_time);
		$model->zpt_end_time = date('Y-m-d',$model->zpt_end_time);
		
		$product = New Product();
		$product->unsetAttributes();//清空属性
		$product->zm_id = Company::model()->getCmpUserId();
		if(isset($_GET['Product']))
			$product->attributes = $_GET['Product'];

		if(isset($_POST['ProTuan'])){
			$model->attributes = $_POST['ProTuan'];
			$model->zpt_start_time = strtotime($model->zpt_start_time);
			$model->zpt_end_time = strtotime($model->zpt_end_time);
			if($model->save()){
				//成功跳转
				$this->redirect(array($this->pre_module_url.'/promotion/snapup'));
			}
		}
		
		$this->render('snapup_create',array(
				'model'=>$model,
				'product'=>$product,
		));
	}
	
	/**
	 * 启动停止抢购秒杀
	 */
	public function actionChangeSnapup($id){
		$model = ProTuan::model()->findByPk($id);
	
		if($model->zpt_close == 1){
			$model->zpt_close = 2;
		}elseif($model->zpt_close == 2){
			$model->zpt_close = 1;
		}
		if($model->save()){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}
	
	/**
	 * 删除抢购秒杀
	 */
	public function actionDeleteSnapup($id){
		$model = ProTuan::model()->findByPk($id);
	
		if($model->zpt_close == 2||$model->zpt_end_time<time()){
			$model->zpt_close = 3;//是否关闭1否2是3删除
		}else{
			throw new CHttpException(404,'该活动还未关闭，不可删除！');
		}
		if($model->save()){
			//成功跳转
			$this->redirect(array($this->pre_module_url.'/promotion/snapup'));
		}
	}
	
	/**
	 * 优惠券管理
	 */
	public function actionCoupon(){
		$model = new Ticket('search');
		$model->unsetAttributes();//清除属性
		$model->zm_id = Company::model()->getCmpUserId();//当前会员
		$model->zti_status = array(1,2);//状态不为删除
		
		if(isset($_GET['Ticket']))
			$model->attributes = $_GET['Ticket'];
		
		$this->render('coupon',array(
				'model'=>$model,
		));
	}
	
	/**
	 * 创建优惠券
	 */
	public function actionCreateCoupon(){
		$model = new Ticket();
		
		if(isset($_POST['Ticket'])){
			$model->attributes = $_POST['Ticket'];
			//当使用条件为不限时，清空条件
			$model->zti_start_time = strtotime($_POST['Ticket']['zti_start_time']);
			$model->zti_end_time = strtotime($_POST['Ticket']['zti_end_time']);
			$model->inputtime = time();
			if($model->zti_start_time <= $model->inputtime ){
				$model->zti_status = 1;
			}
			if($_POST['condition']==1){
				$model->zti_condition = '';
			}
			if($model->save()){
				//成功跳转
				$this->redirect(array($this->pre_module_url.'/promotion/coupon'));
			}
		}
		
		$this->render('coupon_create',array(
				'model'=>$model,
		));
	}

	
	/**
	 * 修改优惠券
	 */
	public function actionUpdateCoupon($id){
		$model = Ticket::model()->findByPk($id);
		$model->zti_start_time = date('Y-m-d H:i',$model->zti_start_time);
		$model->zti_end_time = date('Y-m-d H:i',$model->zti_end_time);
		
		
		if(isset($_POST['Ticket'])){
			$model->attributes = $_POST['Ticket'];
			$model->zti_end_time = strtotime($model->zti_end_time);
			$model->zti_start_time = strtotime($model->zti_start_time);
			$model->updatetime = time();

			if($model->save()){
				//成功跳转
				$this->redirect(array($this->pre_module_url.'/promotion/coupon'));
			}
		}
		
		$this->render('coupon_create',array(
				'model'=>$model,
		));
	}
	/**
	 * 删除优惠券
	 */
	public function actionDeleteCoupon($id){
		$model = Ticket::model()->findByPk($id);
		$model->zti_status = 3;
		if($model->save()){
				//成功跳转
				$this->redirect(array($this->pre_module_url.'/promotion/coupon'));
		}
	}
	
	/**
	 * 启动停止优惠券
	 */
	public function actionChangeCoupon($id){
		$model = Ticket::model()->findByPk($id);
	
		if($model->zti_status == 1){
			$model->zti_status = 2;
		}elseif($model->zti_status == 2){
			$model->zti_status = 1;
		}
		if($model->save()){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}
	
}